package com.erp.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.DepartmentDAO;
import com.erp.domain.TDepartment;
import com.erp.dto.Department;
import com.erp.dto.Page;

@Service(value = "departmentService")
@Transactional
public class DepartmentService {

	private DepartmentDAO departmentDAO = null;

	public DepartmentDAO getDepartmentDAO() {
		return departmentDAO;
	}

	@Resource(name = "departmentDAO")
	public void setDepartmentDAO(DepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public List<Department> listAll() {
		List<TDepartment> TDepartmentList = new ArrayList<TDepartment>(0);
		List<Department> departmentList = new ArrayList<Department>(0);
		TDepartmentList = departmentDAO.listAll();
		if (TDepartmentList != null) {
			for (int i = 0; i < TDepartmentList.size(); i++) {
				departmentList.add(new Department(TDepartmentList.get(i)));
			}
		}
		return departmentList;
	}

	public List<Department> listByLevel(Integer level) {
		List<TDepartment> TDepartmentList = new ArrayList<TDepartment>(0);
		List<Department> departmentList = new ArrayList<Department>(0);
		TDepartmentList = departmentDAO
				.list("from TDepartment department where department.level="
						+ level);
		if (TDepartmentList != null) {
			for (int i = 0; i < TDepartmentList.size(); i++) {
				departmentList.add(new Department(TDepartmentList.get(i)));
			}
		}
		return departmentList;
	}

	public void add(Department department) {
		TDepartment TDepartment = new TDepartment(department);
		TDepartment pTDepartment = departmentDAO.getById(department
				.getParentId());
		if (pTDepartment != null) {
			TDepartment.setLevel(pTDepartment.getLevel() + 1);
			TDepartment.setParent(pTDepartment);
		} else {
			TDepartment.setLevel(1);
			TDepartment.setParent(null);
		}

		departmentDAO.save(TDepartment);
	}

	public Department getById(Integer id) {
		return new Department(departmentDAO.getById(id));
	}

	public void update(Department department) {
		TDepartment TDepartment = departmentDAO.getById(department.getId());
		TDepartment.setName(department.getName());
		TDepartment pTDepartment = departmentDAO.getById(department
				.getParentId());
		if (pTDepartment != null) {
			TDepartment.setLevel(pTDepartment.getLevel() + 1);
			TDepartment.setParent(pTDepartment);
		} else {
			TDepartment.setLevel(1);
			TDepartment.setParent(null);
		}
		TDepartment.setDescription(department.getDescription());
		departmentDAO.update(TDepartment);
	}

	public void delete(Department department) {
		departmentDAO.deleteById(department.getId());
	}

	public List<Department> getPagedList(Page page) {
		// 开始页码
		int from = (page.getPageIndex() - 1) * page.getPageSize();
		int totalRecord = departmentDAO.countAll().intValue();
		page.setTotalRecord(totalRecord);
		List<Department> departmentList = new ArrayList<Department>(0);
		int totalPage = 1;
		if (totalRecord > 0) {
			departmentList = new ArrayList<Department>();
			List<TDepartment> tDepartmentList;
			tDepartmentList = departmentDAO.paging(from, page.getPageSize());
			// 设置总页数
			totalPage = totalRecord % (page.getPageSize()) > 0 ? totalRecord
					/ (page.getPageSize()) + 1 : totalRecord
					/ (page.getPageSize());
			for (int i = 0; i < tDepartmentList.size(); i++) {
				departmentList.add(new Department(tDepartmentList.get(i)));
			}
		}
		page.setTotalPage(totalPage);
		return departmentList;
	}

}
